//
//  SAMKeychain+MMWPTUtil.m
//  MMWPT
//
//  Created by 高得华 on 2018/9/4.
//  Copyright © 2018年 高得华. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SAMKeychain+MMWPTUtil.h"

/// 登录账号的key
static NSString *const MMWPT_RAW_LOGIN = @"MMWPT_RAW_LOGIN";
static NSString *const MMWPT_SERVICE_NAME_IN_KEYCHAIN = @"com.Gdhmmw.zhiyin";
static NSString *const MMWPT_DEVICEID_ACCOUNT         = @"DeviceID";

@implementation SAMKeychain (MMWPTUtil)

+ (NSString *)rawLogin {
    return [[NSUserDefaults standardUserDefaults] objectForKey:MMWPT_RAW_LOGIN];
}

+ (BOOL)setRawLogin:(NSString *)rawLogin {
    if (rawLogin == nil) NSLog(@"+setRawLogin: %@", rawLogin);
    [[NSUserDefaults standardUserDefaults] setObject:rawLogin forKey:MMWPT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

+ (BOOL)deleteRawLogin {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:MMWPT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

+ (NSString *)deviceId {
    NSString * deviceidStr = [SAMKeychain passwordForService:MMWPT_SERVICE_NAME_IN_KEYCHAIN account:MMWPT_DEVICEID_ACCOUNT];
    if (deviceidStr == nil) {
        deviceidStr = [UIDevice currentDevice].identifierForVendor.UUIDString;
        [SAMKeychain setPassword:deviceidStr forService:MMWPT_SERVICE_NAME_IN_KEYCHAIN account:MMWPT_DEVICEID_ACCOUNT];
    }
    return deviceidStr;
}

@end
